package com.app.qtjk.lsh.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.app.common.dataDictionary.DE.DE_USERWEALTH;
import com.app.common.dataDictionary.PO.TUserPO;
import com.app.common.utils.CommonParamProperties;
import com.app.common.utils.FilterUtil;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class CommonService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(CommonService.class);

	public CommonService() throws Exception {
		super();
	}


	/**
	 * 得到首页信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "CommonService_genHomePage")
	public IResData genHomePage(IReqData req) throws Exception {

		log.debug("开始进入 CommonService_genHomePage 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			SQLParam param=new SQLParam();
			String userId = ReqJsonUtil.getPostParam(req,"userId");
			param.addParam(userId);
			//获取用户财富值
			int userwealth = (Integer) dao.queryColumnByKey("CommonService_getUserWealthById", param, 2);
			String hyswitch = CommonParamProperties.getParam("hyswitch");
			int userAmount = Integer.parseInt(StringUtil.parse2String(dao.queryColumnByKey("CommonService_getUserAmount", null, 1)));
			if ("1".equals(hyswitch)) {
				userAmount = userAmount + Integer.parseInt(CommonParamProperties.getParam("hycount"));
			}
			//获取财富奖区数据
			List wealthareaMapList = dao.queryMapListByKey("WealthareaManageService_initWealthareaList", null);
			//获取分类数据
			List categoryMapList = dao.queryMapListByKey("CategoryManageService_initCategoryListQtjk", null);

			hashMap.put("returnStatus", "true");
			hashMap.put("msg", "获取首页成功！");
			hashMap.put("userAmount", userAmount);
			hashMap.put("userwealth", userwealth);
			commonMap.put("wealthareaMapList", wealthareaMapList);
			commonMap.put("categoryMapList", categoryMapList);
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "系统异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;

	}
	
	
	/**
	 * 单独接口获取首页的用户会员数和用户财富值
	 * @param userId
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "CommonService_getSyPd")
	public IResData getSyPd(IReqData req) throws Exception {

		log.debug("开始进入 CommonService_getSyPd 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			SQLParam param=new SQLParam();
			String userId = ReqJsonUtil.getPostParam(req,"userId");
			param.addParam(userId);
			//获取用户财富值
			int userwealth = (Integer) dao.queryColumnByKey("CommonService_getUserWealthById", param, 2);
			//获取注册会员数
			int userAmount = Integer.parseInt(dao.queryColumnByKey("CommonService_getUserAmount", null, 1).toString());

			hashMap.put("returnStatus", "true");
			hashMap.put("msg", "获取首页数据成功！");
			hashMap.put("userAmount", userAmount);
			hashMap.put("userwealth", userwealth);
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "系统异常！");
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;

	}		
	
	/**
	 * 给用户增加一积分
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "CommonService_addOneWealth")
	public IResData addOneWealth(IReqData req) throws Exception{

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		log.debug("开始进入 CommonService_addOneWealth 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
				hashMap.put("returnStatus", "true");
				String userId = ReqJsonUtil.getPostParam(req,"userId");
				SQLParam param = new SQLParam();
				param.addParam(userId);
				TUserPO userPO = dao.queryPOByKey("UserService_getUserPOByID", param, TUserPO.class);
				userPO.setUserwealth(new DE_USERWEALTH(userPO.getUserwealth().value+1));
				int i = dao.updatePO(userPO);
				if (i > 0) {
					hashMap.put("success", "true");
				}else {
					hashMap.put("success", "false");
				}
			}
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}
	
	/**
	 * 给用户增加一积分
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "CommonService_reduceOneWealth")
	public IResData reduceOneWealth(IReqData req) throws Exception{

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		log.debug("开始进入 CommonService_reduceOneWealth 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
				hashMap.put("returnStatus", "true");
				String userId = ReqJsonUtil.getPostParam(req,"userId");
				SQLParam param = new SQLParam();
				param.addParam(userId);
				TUserPO userPO = dao.queryPOByKey("UserService_getUserPOByID", param, TUserPO.class);
				if (userPO.getUserwealth().value == 0) {
					hashMap.put("success", "false");
				}else {
					userPO.setUserwealth(new DE_USERWEALTH(userPO.getUserwealth().value-1));
					int i = dao.updatePO(userPO);
					if (i > 0) {
						hashMap.put("success", "true");
					}else {
						hashMap.put("success", "false");
					}
				}
			}
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}

}